import {
  Body,
  Controller,
  Get,
  Param,
  Post,
  Query,
  UseGuards,
} from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { SysServiceImpl } from '../service/impl/sysServiceImpl';
import {
  SysDeleteManyDto,
  SysFindByIdDto,
  SysInsertDto,
  SysSetStateDto,
  SysUpdateDto,
  SysListPageOrSortDto,
  SysFindManyByFiledDto,
  SysLikeDto,
  tokenDto,
} from '../dto/sys.dto';
import { generateToken, getUserFromToken } from '@/lib/jwt/token';

@ApiTags('系统管理')
@Controller('sys/log')
@ApiBearerAuth()
export class SysController {
  constructor(private service: SysServiceImpl) {}

  /**
   * 新增一条
   * @param body
   */
  @ApiOperation({ description: '创建日志' })
  @Post('/insert')
  async insert(@Body() body: SysInsertDto): Promise<unknown> {
    return this.service.save(body);
  }

  /**
   * 根据id删除多条日志
   * @param body
   */
  @ApiOperation({ description: '根据id删除多条日志' })
  @Post('/deleteMany')
  async deleteMany(@Body() body: SysDeleteManyDto): Promise<unknown> {
    const { ids } = body;
    return await this.service.deleteByIds(ids);
  }

  /**
   * 根据id删除一条日志
   * @param body
   */
  @ApiOperation({ description: '根据id删除一条日志' })
  @Post('/deleteOneById')
  async deleteOneById(@Body() body: SysFindByIdDto): Promise<unknown> {
    const { id } = body;
    return await this.service.deleteOneById(id);
  }

  /**
   * 根据id查找一条日志
   */
  @ApiOperation({ description: '根据id查找一条日志' })
  @Get('/find/:id')
  async findOneById(@Param() params: SysFindByIdDto): Promise<unknown> {
    const { id } = params;
    return await this.service.getOneById(id);
  }

  /**
   * 分页返回数据并排序
   * @param body
   */
  @ApiOperation({ description: '分页返回数据并排序' })
  @Get('/findAllWithPageOrSort')
  async findAllWithPageOrSort(
    @Query() query: SysListPageOrSortDto,
  ): Promise<any> {
    // console.log(query);
    const { field, sort, pageSize, pageNumber } = query;
    /*  console.log(
      `field：${field}, sort:${sort}, pageSize:${pageSize}, pageNumber:${pageNumber}`,
    ); */
    return await this.service.getAllWithPageOrOrder(
      field,
      sort,
      pageSize,
      pageNumber,
    );
  }
  /**
   * 更新一条日志
   * @param body
   */
  @ApiOperation({ description: '更新一条日志' })
  @Post('/update')
  async update(@Body() body: SysUpdateDto): Promise<unknown> {
    const { id, newData } = body;
    return await this.service.update(id, newData);
  }

  /**
   * 根据id设置状态
   * @param body
   */
  @ApiOperation({ description: '根据id设置状态' })
  @Post('/setState')
  async setState(@Body() body: SysSetStateDto): Promise<unknown> {
    const { id, state } = body;
    return await this.service.setStatus(id, state);
  }

  /**
   * 根据id设置状态
   * @param body
   */
  @ApiOperation({ description: '根据字段查询' })
  @Get('/findRole')
  async findRole(@Query() query: SysFindManyByFiledDto): Promise<unknown> {
    const { field, value, pageSize, pageNumber, sort, condition } = query;
    return await this.service.findManyByField(
      field,
      value,
      pageSize,
      pageNumber,
      sort,
      condition,
    );
  }

  /**
   * 根据id设置状态
   * @param body
   */
  @ApiOperation({ description: 'liek查询' })
  @Get('/like')
  async like(@Query() query: SysLikeDto): Promise<unknown> {
    const { field, value, pageSize, pageNumber } = query;
    return await this.service.search(field, value, pageSize, pageNumber);
  }

  /**
   * 生成token
   *
   */
  @ApiOperation({ description: 'token' })
  @Get('/token')
  async token(): Promise<unknown> {
    return await generateToken({ name: 'wangjing', password: 'wangjing123' });
  }
  /**
   * 生成token
   *
   */
  @ApiOperation({ description: 'token' })
  @Get('/verify')
  async verify(@Query() query: tokenDto): Promise<unknown> {
    const { token } = query;
    return await getUserFromToken(token);
  }
}
